/*
 * $Id$
 *
 * Copyright ( c ) 2014 Carefx Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of Carefx
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with Carefx Corporation or a Carefx
 * authorized reseller (the "License Agreement"). Carefx may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, CAREFX DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. CAREFX DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * CAREFX SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF CAREFX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. CAREFX' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE CAREFX SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.common.client.commom;

import java.util.HashMap;
import java.util.Map;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Component;

/**
 * 焦点事件类
 * 
 * @author 杨冲
 */
public class FocusEvents
{

    /** 存储组件的Map */
    private final Map <String, Component> m_componentMap;
    /** 存储focus状态的Map */
    private final Map <String, Boolean> m_statusMap;

    /**
     * 构造函数
     */
    public FocusEvents ()
    {
        m_componentMap = new HashMap <String, Component> ();
        m_statusMap = new HashMap <String, Boolean> ();
    }

    /**
     * 添加组件
     * 
     * @param key
     * @param component
     */
    public void add (final String key, final Component component)
    {
        m_componentMap.put (key, component);
        m_statusMap.put (key, false);
        component.addListener (Events.OnFocus, new Listener <BaseEvent> ()
        {

            @Override
            public void handleEvent (final BaseEvent be)
            {
                m_statusMap.put (key, true);
            }
        });

        component.addListener (Events.OnBlur, new Listener <BaseEvent> ()
        {

            @Override
            public void handleEvent (final BaseEvent be)
            {
                m_statusMap.put (key, false);
            }
        });
    }

    /**
     * 获取焦点所在的组件
     * 
     * @return
     */
    public Component getFocusComponent ()
    {
        for (final Map.Entry <String, Boolean> entry : m_statusMap.entrySet ())
        {
            if (entry.getValue ())
            {
                return m_componentMap.get (entry.getKey ());
            }
        }
        return null;
    }

    /**
     * 获取是否被焦点
     * 
     * @param key
     * @return
     */
    public boolean getFocusStatus (final String key)
    {
        return m_statusMap.get (key);
    }
}
